package com.haoran.dwz.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {
	@Value("${cors.pathPattern:api/**}")
	private String pathPattern;

	@Value("${cors.allowedOrigins:*}")
	private String[] allowedOrigins;

	@Value("${cors.allowedMethods:POST,GET,PUT,DELETE,OPTIONS}")
	private String[] allowedMethods;

	@Value("${cors.maxAge:2592000}")
	private long maxAge;

	@Value("${cors.allowCredentials:false}")
	private boolean allowCredentials;

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping(pathPattern).allowedOrigins(allowedOrigins).allowedMethods(allowedMethods).maxAge(maxAge)
				.allowCredentials(allowCredentials);
	}
}